/**
 * Created by ZFL on 2017/12/26.
 */

function appInit() {
    PageInit();
}
function PageInit() {
    //加载全部优惠券
    myCoupons();//我的优惠券
    infinite();
}
function codeOk() {
    if ($("#code").val().replace(/(^\s*)|(\s*$)/g, "")==""){
        YDUI.dialog.toast('兑换码不能为空','none',function(){});
    }else{
        $.ajaxHandler({
            model:function () {
                var obj ={};
                obj.redeemCode =$("#code").val().replace(/(^\s*)|(\s*$)/g, "");
                obj.userId = com_obj.userId;
                return obj;
            },
            visibleTime:100,
            dataType:"json",
            approve:true,
            api:'yg_baijiaapp/redeemCode/useRedeemCode',
            success:function (result) {
                //console.log(result);
                $("#code").val("");
                if(result.code==10000){
                    YDUI.dialog.toast('兑换成功','success',1000);
                    window.location.reload();
                }else{
                    YDUI.dialog.toast(result.message,'error',1000);
                }
            }
        });
    }
}

var app = new Vue({
    el: '#app',
    data: {
        money:0,
        pageArr:{},
        coupons:[]
    },
    mounted:function(){
        this.$nextTick(function(){
        })
    },
    methods: {
        show: function () {

        },
        info:function() {

        },
        //跳转优惠券使用页面
        goPage:function(item){
            var _this = this;
            _this.pageArr = {
                "3": '../brand/br_active.html?brandId='+item.brandId+'',
                "5": '../../b2c/goods/goods.html?goodsId='+item.goodsId+''
            };
            if(item.type == 3 || item.type == 5){
                window.location.href = _this.pageArr[item.type];
            }else {
                window.location.href = '/';
            }
        }
    },
    filters:{
        discount:function (value) {
            var content =0;
            if(this.isDiscount==1){
                content = fmoneyStr(divideNumber(value,10));
            }else{
                content = fmoneyStr(divideNumber(value,100));
            }
            return content;
        },
        timeStr:function (value) {
            if(value&&value!=''){
                return value.toString().substr(0,10);
            }else{
                return "";
            }
        },
        moneyName:function (value) {
            if(value==0){
                return "元";
            }else{
                return "折";
            }
        }
    }
});

//页面加载我的优惠券
var loading2 = false;
var page2 = 1;
var pageSize2 = 15;
function myCoupons() {
    $.ajaxHandler({
        model: function () {
            var obj = {};
            obj.pageNum = page2;
            obj.pageSize = pageSize2;
            obj.userId = com_obj.userId;
            return obj;
        },
        visibleTime:0,
        visible: 'show',
        approve: true,
        api: "yg_baijiaapp/userJournal/listUserPromotion",
        success: function (result) {
            //console.log(result);
            if (result.code == 10000) {
                var data = result.data;
                //console.log(data);
                if (data.length > 0) {
                    $.each(data,function (index,item) {
                        app.coupons.push(item);
                    });
                    if(loading2){
                        $(".weui-infinite-scroll-box").hide();
                        loading2=false;
                    }
                }else{
                    $(document.body).destroyInfinite();
                    if (page2>1) {
                        YDUI.dialog.toast('没有数据啦~','none',1000);
                    }
                }
            } else {
                YDUI.dialog.toast(result.message,'error',1000);
            }

        }
    });
}


//滚动加载
function infinite() {
    $(document.body).infinite().on("infinite", function() {
        // 如果正在加载，则退出
        if (loading2) return;
        // 设置flag
        loading2 = true;
        //YDUI.dialog.loading.open('正在加载...');
        setTimeout(function () {
            YDUI.dialog.loading.close();
            page2++;
            myCoupons();
        }, 1000);
    });
}